<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>钩子函数</title>
        <style>
            .mask {
                width: 100%;
                height: 100%;
                background-color: rgba(0, 0, 0, 0.5);
                position: fixed;
                left: 0;
                top: 0;
                display: none;
            }

            .mask > img {
                width: 200px;
                height: 150px;
                position: absolute;
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
                border-radius: 20px;
            }
        </style>
    </head>
    <body>
        <div class="mask">
            <img src="../imgs/loading2.gif" alt="" />
        </div>

        <button id="btn1">发送GET请求</button>
        <button id="btn2">发送POST请求</button>
        <button id="btn3">发送ajax请求</button>

        <script src="./jquery/jquery.min.js"></script>

        <script>
            // 请求开始
            $(document).ajaxStart(function () {
                console.log("ajaxStart");
            });

            // 预备发送
            $(document).ajaxSend(function () {
                console.log("ajaxSend");
                $(".mask").css("display", "block");
            });

            // 请求成功
            $(document).ajaxSuccess(function () {
                console.log("ajaxSuccess");
            });

            // 请求失败
            $(document).ajaxError(function () {
                console.log("ajaxError");
            });

            // 请求完成
            $(document).ajaxComplete(function () {
                console.log("ajaxComplete");
                $(".mask").css("display", "none");
            });

            // 请求停止
            $(document).ajaxStop(function () {
                console.log("ajaxStop");
            });
        </script>

        <script>
            $("#btn1").click(function (e) {
                $.get("./server/ajaxGet.php?a=100&b=200");
            });

            $("#btn2").click(function (e) {
                $.post(
                    "./server/ajaxPost.php",
                    // {a:100,b:200},
                    "a=100&b=200",
                    function (data, status, xhr) {
                        console.log(data);
                        console.log(status);
                        console.log(xhr);
                    },
                    "json"
                );
            });

            $("#btn3").click(function (e) {
                $.ajax({
                    url: "http://www.myshop.net/server/jsonp.php",
                    data: { a: 123, b: 456 },
                    dataType: "jsonp",
                    jsonp: "cb",
                });
            });
        </script>
    </body>
</html>
